package com.example.lemis.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemis.model.dto.BannerDTO;
import com.example.lemis.model.dto.PageDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.lemis.common.JsonResponse;
import com.example.lemis.service.IBannerService;
import com.example.lemis.model.domain.Banner;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-24
 * @version v1.0
 */
@RestController
@RequestMapping("/api/banner")
public class BannerController {

    private final Logger logger = LoggerFactory.getLogger( BannerController.class );

    @Autowired
    private IBannerService bannerService;


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<Banner> getById(@PathVariable("id") Long id)throws Exception {
        Banner banner = bannerService.getById(id);
        return JsonResponse.success(banner);
    }

    /**
     * 先上传新版面图片
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public JsonResponse<?> upload(@RequestBody MultipartFile file) throws Exception {
        Map upload = bannerService.upload(file);
        return JsonResponse.success(upload);
    }


    /**
     * 描述：将新图片上传至数据库
     * @param banner
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public JsonResponse<?> add(@RequestBody Banner banner) throws Exception {
        Banner banner1 = bannerService.add(banner);
        return JsonResponse.success(banner1);
    }

    @GetMapping("/list")
    public JsonResponse<?> BannerList(BannerDTO bannerDTO, PageDTO pageDTO) throws Exception{
        Page<BannerDTO> page = bannerService.BannerList(bannerDTO, pageDTO);
        return JsonResponse.success(page);
    }
}

